using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;


//前台用户信息
namespace ApiProject.Api.Controllers
{
    //[Authorize]
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {
        private IRepository<Users> _usersRepository;

        private IRepository<UserInfo> _userInfoRepository;

        private readonly IConfiguration _configuration;

        private readonly IWebHostEnvironment _hostingEnvironment;

        public UserController(
            IConfiguration configuration,
            IRepository<Users> usersRepository,
            IRepository<UserInfo> userInfoRepository,
            IWebHostEnvironment hostingEnvironment
        )
        {
            _usersRepository = usersRepository;
            _configuration = configuration;
            _userInfoRepository = userInfoRepository;
            _hostingEnvironment = hostingEnvironment;
        }


        // 前台用户登录
        // post /user/login
        // 参数 username、password
        [HttpPost, Route("login")]
        public dynamic GetToken(NewUser model)
        {
            var username = model.Username.Trim();
            var password = model.Password.Trim();

            var user = _usersRepository.Table.Where(x => x.Username == username && x.Password == password).FirstOrDefault();
        
            if (user == null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = user,
                    Msg = "用户名或密码错误,请重新输入"
                });
            }
            else
            {
                if (user.IsDelete == false)
                {
                    if (user.IsEnable == true)
                    {
                        return JsonHelper.Serialize(new
                        {
                            Code = 200,
                            Data = new
                            { 
                                Username = username,
                                UserId = user.Id
                            },
                            Msg = "用户登录成功"
                        });
                    }
                    else
                    {
                        return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "用户已被禁用，请联系管理员" });
                    }
                }
                else
                {
                    return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "用户已被删除，请联系管理员" });
                }
            }
        }




        //前台用户修改密码
        [HttpPost("{id}")]
        public dynamic modUserpass(int id, NewUserPass model)
        {
            var pass = model.BeforeuserPass.Trim();
            var password = model.Password.Trim();
            var CheckPass = model.CheckPass.Trim();
            var userpass = _usersRepository.Table.Where(x => x.Password == pass).FirstOrDefault();
            System.Console.WriteLine(userpass);
            if (userpass != null)
            {
                var user = _usersRepository.GetById(id);
                if (user != null)
                {
                    user.Password = password;

                    user.UpdateTime = DateTime.Now;
                    _usersRepository.Update(user);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = user,
                        Msg = string.Format("修改成功，请注意查收")
                    });
                }
                else
                {
                    return JsonHelper.Serialize(new
                    {
                        Code = 104,
                        Data = "",
                        Msg = "指定Id的用户不存在，请确认后重试"
                    });
                }


            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 104,
                    Data = "",
                    Msg = "原密码错误，请确认后重试"
                });
            }

        }


        // 前台用户 修改 用户信息
        // Put:/user/alter_userinfo/{id}
        //参数：id = userId userInfo
        [HttpPost, Route("alter_userinfo/{id}")]
        public dynamic alterUserInfo(int id, NewUserInfo newUserInfo)
        {

            var userInfo = _userInfoRepository.GetById(id);

            if (userInfo != null)
            {

                userInfo.NickName = newUserInfo.NickName;
                userInfo.Sex = newUserInfo.Sex;
                userInfo.Birthday = newUserInfo.Birthday;
                userInfo.Address = newUserInfo.Address;
                userInfo.SelfIntroduction = newUserInfo.SelfIntroduction;


                _userInfoRepository.Update(userInfo);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = userInfo,
                    Msg = "用户信息修改成功"
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "用户信息修改失败"
                });
            }

        }


        // 前台显示当前登录用户的名片信息
        // Get: /user/get_userinfo/{id}
        [HttpGet, Route("getUserinfo/{id}")]
        public dynamic getUserInfo(int id)
        {

            var userInfo = _userInfoRepository.GetById(id);

            if (userInfo != null)
            {
                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = userInfo,
                    Msg = "获取用户信息成功"
                });
            }
            else
            {
                return JsonHelper.Serialize(new
                {
                    Code = 1000,
                    Data = "",
                    Msg = "获取用户信息失败"
                });
            }
        }

    }
}
